template.defaults.imports.splitImages = function (imgsStr) {
    var imgs = imgsStr.split(',');
    template.defaults.imports.imgs = imgs;
};

template.defaults.imports.TimestampToTime = function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes();
    return Y + M + D + h + m ;
};

//下拉刷新
$(".content").pullToRefresh().on("pull-to-refresh", function () {
    $("#answers-content").html("");
    resetCondition();
    queryList(function () {
        $(".content").pullToRefreshDone(); // 重置下拉刷新
    });
    return false;
});

//上拉加载更多
$('.content').infinite().on("infinite", function () {
    if (hasNextPage) {
        pageNo++;
        queryList(function () {}, true);
    }
});

//复位所有参数
/* global cate1 resetCate cate2 attrs*/
function resetCondition() {
    pageNo = 1;
}

var loading = false; //状态标记
var hasNextPage = false;
var pageNo = 1;
queryList();
function queryList(callback, isMore) {
    if (loading) return;
    loading = true;    
    //查询当前帖子回复信息
    $.post('/antique/api/mallPosts/query_reply_list',{
        postsId:id,
        start:(pageNo - 1) * 10,
        limit:10
    },function(result){
        if(isMore){
            $('#answers-content').append(template('answers-more', {
                list:result.data.list
            }));
        } else {
            $('#answers-content').append(template('answers', {
                list:result.data.list,
            }));
        }

        //绑定回复帖子事件
        $('.answer .container .time .msg').click(function(){
            replyId = $(this).attr('replyId');
            replyUserName = $(this).attr('replyUserName');
            $('.replyFrame').show();
            $('#replyUserName').text($(this).attr('replyUserName'));
            $('.send input').focus();
        });

        hasNextPage = result.data.hasNextPage;
        if (!result.data.hasNextPage) {
            $('#loadmore').hide();
            $('#nomore').show();
        } else {
            $('#loadmore').show();
            $('#nomore').hide();
        }
        if (callback) callback();
        loading = false;
    });
}

$.get('/antique/api/mallPosts/web/query', {
    id: id
}, function (result) {
    $('#post-content').append(template('post', {
        post:result.data
    }));
    loading = false;
    var images = result.data.image.split(',');
    $(".post .container .photos .photo").click(function() {
        var currentUrl = $(this).data('url');
        wx.previewImage({
            current: currentUrl, // 当前显示图片的http链接
            urls: images // 需要预览的图片http链接列表
        }); 
    });
});

//发送留言
var isSendLeaving = false;
$('#goSend').click(function(){
    if(isSendLeaving)return;
    isSendLeaving = true;
    var content = $('input[name="send-text"]').val();
    if (content == '' || !content){
        return false;
    }
    $.post('/antique/api/mallPosts/reply_posts', {
        token:getCookie('token'),
        postsId:id,
        replyId:(typeof(replyId) != 'undefined' && replyId)?replyId:'',
        content:content
    }, function (result) {
        if (result.code == 0) {
            $.toast("上传成功!谢谢您的使用!");
            window.location.reload();
        } else {
            $.toast(result.message, "cancel");
        }
        isSendLeaving = false;
    });
});

$('.replyFrame .close').click(function(){
    $(this).parent().hide();
    replyId = false;
});

$('.send input').on('blur', function () {
    window.scroll(0, 0);
}).on('focus', function () {
});